package com.process.config.Liquibase;

import com.alibaba.druid.pool.DruidDataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
public class LiquibaseConfiguration {

    /**
     * 主数据源版本管理
     *
     * @param
     * @return
     */
    @Bean
    public SpringLiquibase masterliquibase(LiquibaseProp masterProperties) {
        return LiquibaseInit(masterProperties);
    }

    /**
     * liquibase初始化数据库
     *
     * @param properties
     * @return
     */
    private SpringLiquibase LiquibaseInit(LiquibaseProp properties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());
        druidDataSource.setUrl(properties.getUrl());

        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(druidDataSource);
        liquibase.setChangeLog(getChangelog(properties.getIsSyncDBSchema(), properties.getConf()));
        liquibase.setContexts("development,test,production");
        liquibase.setShouldRun(true);
        liquibase.setDefaultSchema(properties.getDefaultSchema());
        return liquibase;
    }

    /**
     * 获取数据库差异文件
     *
     * @param isSyncDBSchema 是否同步表结构
     * @param conf           //liquibase配置文件
     * @return
     */
    private String getChangelog(String isSyncDBSchema, String conf) {
        String defaultChangelog = "classpath:liquibase/empty.xml";

        if ((!StringUtils.isEmpty(isSyncDBSchema)) && (!StringUtils.isEmpty(conf))) {
            if (isSyncDBSchema.toLowerCase().equals("true"))
                defaultChangelog = conf;
        }
        return defaultChangelog;
    }

}